package com.ewhale.feitengguest.ui.user.message;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.presenter.user.message.BizChatPresenter;
import com.ewhale.feitengguest.ui.user.MainActivity;
import com.ewhale.feitengguest.ui.user.message.chat.AuthHelper;
import com.ewhale.feitengguest.ui.user.message.chat.Constant;
import com.ewhale.feitengguest.view.user.message.BizChatView;
import com.hyphenate.helpdesk.easeui.recorder.MediaManager;
import com.hyphenate.helpdesk.easeui.util.CommonUtils;
import com.hyphenate.helpdesk.easeui.util.Config;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.hyphenate.helpdesk.model.ContentFactory;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.HawkKey;


public class BizChatActivity extends MBaseActivity<BizChatPresenter> implements BizChatView {
    private CustomChatFragment chatFragment;
    private String toChatUsername;
    private String projectId;
    private String itemUrl;

    /**
     * 与客服聊天
     * <p>
     * //     * @param extBundleBuilder 额外信息
     */
    public static void open(final MBaseActivity context) {
        context.showLoading();
        new Handler().postDelayed(() -> {
            context.dismissLoading();
            if (Hawk.get(HawkKey.USER_HX_ID, "").isEmpty() || Hawk.get(HawkKey.USER_HX_PASSWORD, "").isEmpty()) {
                context.showToast("聊天账号为空，请联系管理员");
                return;
            }
            AuthHelper.getInstance().login(context, Hawk.get(HawkKey.USER_HX_ID, ""),
                    Hawk.get(HawkKey.USER_HX_PASSWORD, ""), new AuthHelper.AuthCallback() {
                        @Override
                        public void onSuccess() {
                            open(context, Constant.DEFAULT_CUSTOMER_ACCOUNT);
                        }

                        @Override
                        public void onError(int errorCode, String errorStr) {
                        }
                    });
        }, 500);
    }

    /**
     * 与客服聊天
     *
     * @param serviceIMNumber 客服IM号
     *                        //     * @param extBundleBuilder 额外信息
     */
    public static void open(MBaseActivity context, String serviceIMNumber) {
        IntentBuilder builder = new IntentBuilder(context)
                .setTargetClass(BizChatActivity.class)
                .setServiceIMNumber(serviceIMNumber)
                .setShowUserNick(true)
                .setVisitorInfo(ContentFactory.createVisitorInfo(null)
                        .phone(Hawk.get(HawkKey.USER_PHONE, ""))
                        .nickName(Hawk.get(HawkKey.USER_NICK_NAME, "")));
        context.startActivity(builder.build());
        context.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        //IM服务号
        toChatUsername = getIntent().getExtras().getString(Config.EXTRA_SERVICE_IM_NUMBER);
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_biz_chat;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("在线客服");
        //可以直接new ChatFragment使用
        chatFragment = new CustomChatFragment();
        //传入参数
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MediaManager.release();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = intent.getStringExtra(Config.EXTRA_SERVICE_IM_NUMBER);
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
        if (CommonUtils.isSingleActivity(this)) {
            Intent intent;
            intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        super.onBackPressed();
    }

    @Override
    public void showProLoading() {

    }

    @Override
    public void dimissProLoading() {

    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {

    }

    public static class ExtBundleBuilder {
        private String title;
        private String orderTitle;
        private String orderDesc;
        private String price;
        private String imageUrl;
        private String projectId;
        private boolean project;//判断项目还是攻略

        public ExtBundleBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public ExtBundleBuilder setOrderTitle(String orderTitle) {
            this.orderTitle = orderTitle;
            return this;
        }

        public ExtBundleBuilder setOrderDesc(String orderDesc) {
            this.orderDesc = orderDesc;
            return this;
        }

        public ExtBundleBuilder setPrice(String price) {
            this.price = price;
            return this;
        }

        public ExtBundleBuilder setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
            return this;
        }

        public ExtBundleBuilder setProjectId(String projectId) {
            this.projectId = projectId;
            return this;
        }

        public ExtBundleBuilder setProject(boolean project) {
            this.project = project;
            return this;
        }

        public Bundle build() {
            Bundle bundle = new Bundle();
            bundle.putString("title", title);
            bundle.putString("orderTitle", orderTitle);
            bundle.putString("orderDesc", orderDesc);
            bundle.putString("price", price);
            bundle.putString("imageUrl", imageUrl);
            bundle.putString("projectId", projectId);
            bundle.putBoolean("project", project);
            return bundle;
        }
    }


    public static int rawX;
    public static int rawY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        rawX = (int) ev.getRawX();
        rawY = (int) ev.getRawY();
        return super.dispatchTouchEvent(ev);
    }
}
